/**
 * shidao
 * FunctionTempldate.java
 * 2016年1月20日
 */
package com.yizhuoyan.his.util;

import com.yizhuoyan.his.exception.HISException;

import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 数据验证工具类
 *
 * @author root@yizhuoyan.com
 */

public interface AssertThrowUtil {


    default void throwException(String message, Object... args) {
        throw new HISException("AssertError", String.format(message, args));
    }


    default String $(String label, String s, Object... args) {
        return assertNotBlank(label + "不能为空", s, args);
    }

    default void assertNull(String message, Object obj, Object... args) {
        if (obj != null) throwException(message, args);
    }

    default void assertNotNull(String message, Object obj, Object... args) {
        if (obj == null)
            throwException(message, args);
    }

    /**
     * 断言字符串是null或者空字符串或空白字符串
     *
     * @param message
     * @param s
     * @param args
     */
    default void assertBlank(String message, String s, Object... args) {
        if (s != null && s.trim().length() != 0) {
            throwException(message, args);
        }
    }

    default String assertNotBlank(String message, String s,
                                  Object... args) {
        if (s == null || (s = s.trim()).length() == 0) {
            throwException(message, args);
        }
        return s;
    }


    default int assertInteger(String message, String intStr,
                              Object... args) {
        if (intStr == null || intStr.trim().length() == 0) {
            throwException(message, args);
        }
        try {
            return Integer.parseInt(intStr);
        } catch (Exception e) {
            throwException(message, args);
        }
        return 0;
    }

    default void assertNotEquals(String message, Object a, Object b,
                                 Object... args) {
        if (a == null ? null == b : a.equals(b)) {
            throwException(message, args);
        }
    }

    default void assertEquals(String message, Object a, Object b,
                              Object... args) {
        if (a == null ? null != b : !a.equals(b)) {
            throwException(message, args);
        }
    }

    default void assertEmpty(String message, Collection b,
                             Object... args) {
        if (b == null || b.size() != 0) {
            throwException(message, args);
        }
    }

    default void assertNotEmpty(String message, Collection b,
                                Object... args) {
        if (b != null && b.size() == 0) {
            throwException(message, args);
        }
    }

    default void assertTrue(String message, boolean b, Object... args) {
        if (!b) {
            throwException(message, args);
        }
    }

    default void assertFalse(String message, boolean b, Object... args) {
        if (b) {
            throwException(message, args);
        }
    }

    default void assertLessThan(String label, int n, int target, Object... args) {
        if (n >= target) {
            throwException(label + "必须小于" + target, args);
        }
    }
    default void assertLessThan(String label, long n, long target, Object... args) {
        if (n >= target) {
            throwException(label + "必须小于" + target, args);
        }
    }

    default void assertGreatThan(String label, int n, int target, Object... args) {
        if (n <= target) {
            throwException(label + "必须大于" + target, args);
        }
    }
    default void assertGreatThan(String label, long n, long target, Object... args) {
        if (n <= target) {
            throwException(label + "必须大于" + target, args);
        }
    }

    default void assertLessThan(String label, String s, int target, Object... args) {
        int len = s == null ? 0 : s.length();
        if (len >= target) {
            throwException(label + "长度不可超过" + target, args);
        }
    }

    default void assertGreatThan(String label, String s, int target, Object... args) {
        int len = s == null ? 0 : s.length();
        if (len <= target) {
            throwException(label + "长度不可小于" + target, args);
        }
    }
    default void assertLessThan(String label, String s, long target, Object... args) {
        int len = s == null ? 0 : s.length();
        if (len >= target) {
            throwException(label + "长度不可超过" + target, args);
        }
    }

    default void assertGreatThan(String label, String s, long target, Object... args) {
        int len = s == null ? 0 : s.length();
        if (len <= target) {
            throwException(label + "长度不可小于" + target, args);
        }
    }

    default void assertIn(String label, Object target, Object[] choices, Object... args) {
        boolean contains = false;
        for (Object choice : choices) {
            if (Objects.equals(target, choice)) {
                contains = true;
                break;
            }
        }
        if (!contains) {
            throwException(label + "的值必须从" + Arrays.toString(choices) + "中选取", args);
        }
    }

    Pattern PATTERN_EMAIL = Pattern.compile("\\w+@[\\w.]+");

    default void assertIsEmail(String label, String target) {
        Matcher matcher = PATTERN_EMAIL.matcher(target);
        if (!matcher.matches()) {
            throwException(label + "不是合格的邮箱形式");
        }
    }



    default void assertAllWordCharacter(String label, String target) {
        char c;
        for (int i = target.length(); i-- > 0; ) {
            c = target.charAt(i);
            if (!(((c >= '0') && (c <= '9')) ||
                    ((c >= 'a') && (c <= 'z')) ||
                    ((c >= 'A') && (c <= 'Z')) ||
                    (c == '-'))) {
                throwException(label + "仅能是字母数字或下划线");
            }
        }

    }


}
